package features.advance.leetcode;

/**
 * @author LIN
 * @date 2021-05-31
 */
public class BitOperation {
    public static void bitOperation(){
        System.out.println("======与&=========");
        System.out.println(1 & 0); // 0
        System.out.println(1 & 1); // 1
        System.out.println(0 & 0); // 0
        System.out.println(0 & 1); // 0

        System.out.println("======或|=========");
        System.out.println(1 | 0); // 1
        System.out.println(1 | 1); // 1
        System.out.println(0 | 0); // 0
        System.out.println(0 | 1); // 1

        System.out.println("======异或^=======");
        System.out.println(1 ^ 0); // 1
        System.out.println(1 ^ 1); // 0
        System.out.println(0 ^ 0); // 0
        System.out.println(0 ^ 1); // 1

        System.out.println(1 << 31);// 左移
        System.out.println(2 >> 1);// 右移
        System.out.println(Integer.toBinaryString(-1 >> 2)+":"+Integer.toBinaryString(-1 >> 2).length()+"位"); // 有符号右移，空位补1
        System.out.println(Integer.toBinaryString(-1)+":"+Integer.toBinaryString(-1).length()+"位");
        System.out.println("-1 >>> 1:无符号右移，空位补0:"+(-1 >>> 1));// 无符号右移，空位补0
        System.out.println(Integer.toBinaryString(-1 >>> 1)+":"+Integer.toBinaryString(-1 >>> 1).length()+"位");
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.toBinaryString(Integer.MIN_VALUE)+":"+Integer.toBinaryString(Integer.MIN_VALUE).length()+"位");
        System.out.println(Integer.toBinaryString(Integer.MAX_VALUE)+":"+Integer.toBinaryString(Integer.MAX_VALUE).length()+"位");



    }

    public static int lowbit(int x){
        System.out.println(Integer.toBinaryString(x));
        System.out.println(Integer.toBinaryString(-x));
        System.out.println(Integer.toBinaryString(x & (-x)));
        return x & (-x);
    }

    public static void main(String[] args) {
        bitOperation();

        System.out.println("lowbit 函数");
        System.out.println(lowbit(34));

        System.out.println(Integer.toBinaryString(101));
        System.out.println(Integer.toBinaryString(27));
    }
}
